package com.achilles.creditsesameringview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

/**
 * TODO: document your custom view class.
 */
public class CreditSesameRingView extends View {

    private static final int ArcWidth = 20;
    private static final int WIDGET_MIN_WIDTH = 400;
    private static final int ArcStartAngle = 45;
    private static final String BETA = "BETA";
    private Paint mArcPaint = new Paint();
    private Paint mArcTextPain = new Paint();
    private RectF mArcRectF = new RectF();
    private int mCenter, mArcRadius;

    public CreditSesameRingView(Context context) {
        this(context, null);
    }

    public CreditSesameRingView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CreditSesameRingView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mArcPaint = new Paint();
        mArcPaint.setStrokeWidth(ArcWidth);
        mArcPaint.setAntiAlias(true);
        mArcPaint.setStyle(Paint.Style.STROKE);
        mArcPaint.setColor(Color.BLUE);

        mArcTextPain = new Paint();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        drawArc(canvas);
        drawText(canvas, 750);
    }

    private void drawText(Canvas canvas, int score) {
        int textMargin = 20;
        mArcTextPain.setTextAlign(Paint.Align.CENTER);
        mArcTextPain.setColor(Color.WHITE);

        double scoreTextHeight = drawScoreText(canvas, score);
        drawBeatText(canvas, textMargin, scoreTextHeight);
        double levelTextHeight = drawLevelText(canvas, score, textMargin, scoreTextHeight);
        drawUpdateText(canvas, textMargin, scoreTextHeight, levelTextHeight);
    }

    private void drawUpdateText(Canvas canvas, int textMargin, double scoreTextHeight, double
            levelTextHeight) {
        float updateTextSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,10,
                getContext().getResources().getDisplayMetrics());
        mArcTextPain.setTextSize(updateTextSize);
        double updateTextHeight = getFontHeight(mArcTextPain);
        String updateTextString = getUpdateString();
        canvas.drawText(updateTextString, mCenter, (float) (mCenter + scoreTextHeight + 2 * textMargin + levelTextHeight + updateTextHeight),
                mArcTextPain);
    }

    private double drawLevelText(Canvas canvas, int score, int textMargin, double scoreTextHeight) {
        float levelTextSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,20,
                getContext().getResources().getDisplayMetrics());
        mArcTextPain.setTextSize(levelTextSize);
        double levelTextHeight = getFontHeight(mArcTextPain);
        String levelTextString = getLevel4Score(score);
        canvas.drawText(levelTextString, mCenter, (float) (mCenter + scoreTextHeight + textMargin + levelTextHeight),
                mArcTextPain);
        return levelTextHeight;
    }

    private void drawBeatText(Canvas canvas, int textMargin, double scoreTextHeight) {
        float beatTextSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 10,
                getContext().getResources().getDisplayMetrics());
        mArcTextPain.setTextSize(beatTextSize);
        double beatTextHeight = getFontHeight(mArcTextPain);
        canvas.drawText(BETA, mCenter, (float) (mCenter - textMargin - scoreTextHeight - beatTextHeight),
                mArcTextPain);
    }

    private String getUpdateString() {
        return "评估时间:2016.09.19";
    }

    private String getLevel4Score(int score) {
        return "信用极好";
    }

    private double drawScoreText(Canvas canvas, int score) {
        float scoreTextSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 38,
                getContext().getResources().getDisplayMetrics());
        mArcTextPain.setTextSize(scoreTextSize);
        double scoreTextHeight = getFontHeight(mArcTextPain);
        canvas.drawText(String.valueOf(score), mCenter, (float) (mCenter + scoreTextHeight / 2),
                mArcTextPain);
        return scoreTextHeight;
    }

    private double getFontHeight(Paint mArcTextPain) {
        return Math.ceil(mArcTextPain.getFontMetrics().descent - mArcPaint.getFontMetrics().ascent);
    }

    private void drawArc(Canvas canvas) {
        canvas.drawArc(mArcRectF, ArcStartAngle, -(180 + 2 * ArcStartAngle), false, mArcPaint);
    }


    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mCenter = w / 2;
        mArcRadius = mCenter / 2;

        mArcRectF.left = mCenter - mArcRadius;
        mArcRectF.top = mCenter - mArcRadius;
        mArcRectF.right = mCenter + mArcRadius;
        mArcRectF.bottom = mCenter + mArcRadius;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);

        int size = WIDGET_MIN_WIDTH;
        if (widthMode == MeasureSpec.AT_MOST && heightMode == MeasureSpec.AT_MOST) {
            size = WIDGET_MIN_WIDTH;
        } else if (widthMode == MeasureSpec.EXACTLY && heightMode == MeasureSpec.EXACTLY) {
            size = Math.min(widthSize, heightSize);
        } else if (widthMode == MeasureSpec.AT_MOST) {
            size = Math.min(WIDGET_MIN_WIDTH, heightSize);
        } else if (heightSize == MeasureSpec.AT_MOST) {
            size = Math.min(WIDGET_MIN_WIDTH, widthSize);
        }

        setMeasuredDimension(size, size);
    }
}
